package main

import (
	"chapter6/models"
	_ "chapter6/routers"
	"fmt"
	"github.com/beego/beego/v2/client/orm"
	_ "github.com/go-sql-driver/mysql"

	beego "github.com/beego/beego/v2/server/web"
)

/**
 * Created with Goland.
 * User: 令狐飞侠
 * Date: 2022-3-10
 * Description: main函数
 */
func main() {
	Init()
	beego.Run()
}

func Init() {
	// 读取配置信息
	host,_ := beego.AppConfig.String("dbhost")
	port,_ := beego.AppConfig.Int("dbport")//数据库端口

	dbname,_  := beego.AppConfig.String("db")//数据库名
	username,_  := beego.AppConfig.String("dbuser")//账号
	password,_  := beego.AppConfig.String("dbpassword")

	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8&loc=Local", username, password, host, port, dbname)

	//注册mysql Driver
	orm.RegisterDriver("mysql", orm.DRMySQL)

	// 注册数据库的连接 default
	orm.RegisterDataBase("default", "mysql", dsn)

	orm.RegisterModel(new(models.User))

	// 打开调试模式，开发的时候方便查看orm生成什么样子的sql语句
	orm.Debug = true
}